import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Animated water & overlay on top of rotterdam map
 * 
 * @author Jasper Jans
 *
 */
public class Flood extends Actor
{
    private GameMap gameMap;
    private boolean animationPlaying = false;
    private boolean reversed;
    private int animationFrame;
        
    public Flood(GameMap gameMap)
    {
        this.gameMap = gameMap;
    }

    public void act() 
    {
        if(animationPlaying)
        {
            setAnimationFrame();
            Greenfoot.delay(20);
        }
 
    }   
    
    public void playAnimation(boolean reversed)
    {
        animationFrame = reversed ? 9 : 1;
        animationPlaying = true;
        this.reversed = reversed;
    }
    
    
    private void setAnimationFrame()
    {
        GreenfootImage nextImage = new GreenfootImage("flood" + Integer.toString(animationFrame) + ".png");
        setImage(nextImage);
        
        if(reversed)
        {
            if(animationFrame -- ==1)
            {
                animationPlaying = false;
                gameMap.showPointers(); 
                gameMap.endGame();
            }
        }else{
            if(animationFrame++ >= 9)
            {
                animationPlaying = false;
                Greenfoot.delay(100);
                gameMap.showPointers();
                setTransparentFlood(); 
            }
        }
        
        if((!reversed && animationFrame++ >= 9) || (reversed && animationFrame -- ==1))
        {
                animationPlaying = false;
                Greenfoot.delay(100);
                gameMap.showPointers();
                
                if(!reversed)
                    setTransparentFlood();

        }
    }
    
    public void setTransparentFlood()
    {
        GreenfootImage img = new GreenfootImage("flood9.png");
        img.setTransparency(220);
        setImage(img);
    }
}
